package com.tuhu.dai.domain.order.service;

import com.alipay.api.AlipayApiException;
import com.tuhu.dai.domain.order.model.aggregate.CreateOrderAggregate;
import com.tuhu.dai.domain.order.model.entity.OrderEntity;
import com.tuhu.dai.domain.order.model.entity.PayOrderEntity;
import com.tuhu.dai.domain.order.model.entity.ProductEntity;
import com.tuhu.dai.domain.order.model.entity.ShopCarEntity;
import com.tuhu.dai.domain.order.model.valobj.OrderStatusVO;
import com.tuhu.dai.domain.order.port.IProductPort;
import com.tuhu.dai.domain.order.repository.IOrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * @author: DaiTao
 * @description:
 * @create: 2025-08-31 20:38
 **/
@Slf4j
public abstract class AbstractOrderService implements IOrderService {


    protected final IOrderRepository orderRepository;

    protected final IProductPort productPort;

    public AbstractOrderService(IOrderRepository orderRepository, IProductPort productPort) {
        this.orderRepository = orderRepository;
        this.productPort = productPort;
    }

    public PayOrderEntity creatOrder(ShopCarEntity shopCarEntity) throws AlipayApiException {
        // 查询是否有未支付的订单
        OrderEntity orderEntity = orderRepository.queryUnPayOrder(shopCarEntity);

        // 如果存在订单，但是已经掉单，需要调用支付宝重新创建
        if(orderEntity != null && Objects.equals(orderEntity.getOrderStatusVO(), OrderStatusVO.PAY_WAIT.getCode())){
            log.info("创建订单-存在，已存在未支付订单。userId:{} productId:{} orderId:{}", shopCarEntity.getUserId(), shopCarEntity.getProductId(), orderEntity.getOrderId());
            return PayOrderEntity.builder()
                    .orderId(orderEntity.getOrderId())
                    .payUrl(orderEntity.getPayUrl())
                    .build();

        }else if(orderEntity != null && Objects.equals(orderEntity.getOrderStatusVO(), OrderStatusVO.CREATE.getCode())) {
            // 掉单
            log.info("创建订单-存在，存在未创建支付单订单，创建支付单开始 userId:{} productId:{} orderId:{}", shopCarEntity.getUserId(), shopCarEntity.getProductId(), orderEntity.getOrderId());
            PayOrderEntity payOrderEntity = doPrepayOrder(shopCarEntity.getUserId(), shopCarEntity.getProductId(), orderEntity.getProductName(), orderEntity.getOrderId(), orderEntity.getTotalAmount());
            return PayOrderEntity.builder()
                    .orderId(payOrderEntity.getOrderId())
                    .payUrl(payOrderEntity.getPayUrl())
                    .build();
        }

        // 创建订单
        // 查询商品信息
        ProductEntity productEntity = productPort.queryProductByProductId(shopCarEntity.getProductId());
        // 创建订单
        OrderEntity entityOfOrder = CreateOrderAggregate.buiderOrderEntity(productEntity.getProductId(), productEntity.getProductName());
        // 创建聚合对象
        CreateOrderAggregate orderAggregate = CreateOrderAggregate.builder()
                .userId(shopCarEntity.getUserId())
                .payProduct(productEntity)
                .orderEntity(entityOfOrder)
                .build();

        // 插入订单信息
        this.doSaveOrder(orderAggregate);

        // 创建支付宝支付但
        PayOrderEntity payOrderEntity = doPrepayOrder(shopCarEntity.getUserId(), productEntity.getProductId(), productEntity.getProductName(), entityOfOrder.getOrderId(), productEntity.getPrice());
        log.info("创建订单-完成，生成支付单。userId: {} orderId: {} payUrl: {}", shopCarEntity.getUserId(), entityOfOrder.getOrderId(), payOrderEntity.getPayUrl());

        return PayOrderEntity.builder()
                .orderId(orderAggregate.getOrderEntity().getOrderId())
                .payUrl(payOrderEntity.getPayUrl())
                .build();

    }

    protected abstract PayOrderEntity doPrepayOrder(String userId, String productId, String productName, String orderId, BigDecimal price) throws AlipayApiException;

    protected abstract void doSaveOrder(CreateOrderAggregate orderAggregate);


}
